<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Bootstrap Material Admin</title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="robots" content="all,follow">
    <!-- Bootstrap CSS-->
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Poppins:300,400,700">
    <link rel="stylesheet" href="css/style.default.css" id="theme-stylesheet">

    <!--和 element-ui/lib/index.js 一起可使用 this.$message.success()-->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">

</head>
<body>
<div class="page login-page" id="app">
    <div class="container d-flex align-items-center">
        <div class="form-holder has-shadow">
            <div class="row">
                <!-- Logo & Information Panel-->
                <div class="col-lg-6">
                    <div class="info d-flex align-items-center">
                        <div class="content">
                            <div class="logo">
                                <h1>欢迎注册</h1>
                            </div>
                            <p>员工管理系统</p>
                        </div>
                    </div>
                </div>
                <!-- Form Panel    -->
                <div class="col-lg-6 bg-white">
                    <div class="form d-flex align-items-center">
                        <div class="content">
                            <div class="form-group">
                                <input id="register-username" v-model="user.username" class="input-material" type="text"
                                       placeholder="请输入用户名/姓名">
                                <div class="invalid-feedback">
                                    用户名必须在2~10位之间
                                </div>
                            </div>
                            <div class="form-group">
                                <input id="register-password" v-model="user.password" class="input-material"
                                       type="password"
                                       placeholder="请输入密码">
                                <div class="invalid-feedback">
                                    密码必须在6~10位之间
                                </div>
                            </div>
                            <div class="form-group">
                                <input id="register-passwords" class="input-material" type="password"
                                       placeholder="确认密码">
                                <div class="invalid-feedback">
                                    两次密码必须相同 且在6~10位之间
                                </div>
                            </div>
                            <div class="form-group">
                                &emsp;&emsp;男<input type="radio" v-model="user.gender" value="true">
                                &emsp;&emsp;女<input type="radio" v-model="user.gender" value="false">
                            </div>
                            <div class="form-group" style="display: flex">
                                <img :src="imgCode" id="img-vcode"
                                     @click="getCodeImage" alt="验证码" style="padding: 0 10px">
                                <input class="input-material" v-model="code" type="text"
                                       placeholder="输入验证码">
                            </div>
                            <div class="form-group">
                                <button id="regbtn" type="button" @click="register" class="btn btn-primary">
                                    注册
                                </button>
                            </div>
                            <small>已有账号?</small><a href="login.html" class="signup">&nbsp;登录</a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

<!--<script src="https://unpkg.com/vue/dist/vue.js"></script>-->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<!-- 和element-ui/lib/theme-chalk/index.css 一起可使用 this.$message.success("登录成功！"); 等操作-->
<script src="https://unpkg.com/element-ui/lib/index.js"></script>

<!--挂载vue-->
<script>
    // Vue.prototype.$http = axios;
    // axios.defaults.baseURI = "http://localhost:8081/ems_vue";
    // 自定义配置：新建一个 axios 实例
    const $http = axios.create({
        baseURL: 'http://localhost:8081/ems_vue',
        timeout: 1000,
        headers: {'X-Custom-Header': 'foobar'}
    });
    let app = new Vue({
        el: "#app",
        data: {
            imgCode: "",
            user: {
                // value="true",传值默认为男，前端传true或false，存入数据库的是1或0
                gender: true
            },
            code: ""
        },
        created() {
            this.getCodeImage()
        },
        methods: {
            //获取验证码
            async getCodeImage() {
                const {data: res} = await $http.get("/getImage");
                // console.log("解构前：" + res.data);
                console.log("后端返回的base64数据：", res);
                this.imgCode = "data:image/png;base64," + res;
            },
            // 注册
            async register() {
                const {data: res} = await $http.post("/register?code=" + this.code, this.user);
                console.log("后端返回的数据：", res);
                if (res.state) {
                    // this.$message.success("注册成功！");
                    alert(res.msg + "点击确定跳转至登录页面");
                    location.href = './login.html';
                } else {
                    // this.$message.error("注册失败");
                    this.$message.error(res.msg);
                    // 该用户已存在！或 验证码错误
                    // alert(res.msg)
                }
            }
        }
    })
</script>

<!-- JavaScript files-->
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script>
    $(function () {
        /*错误class  form-control is-invalid
        正确class  form-control is-valid*/
        var flagName = false;
        var flagPas = false;
        var flagPass = false;
        /*验证用户名*/
        var name, passWord, passWords;
        $("#register-username").change(function () {
            name = $("#register-username").val();
            if (name.length < 2 || name.length > 10) {
                $("#register-username").removeClass("form-control is-valid");
                $("#register-username").addClass("form-control is-invalid");
                flagName = false;
            } else {
                $("#register-username").removeClass("form-control is-invalid");
                $("#register-username").addClass("form-control is-valid");
                flagName = true;
            }
        });
        /*验证密码*/
        $("#register-password").change(function () {
            passWord = $("#register-password").val();
            if (passWord.length < 6 || passWord.length > 18) {
                $("#register-password").removeClass("form-control is-valid");
                $("#register-password").addClass("form-control is-invalid");
                flagPas = false;
            } else {
                $("#register-password").removeClass("form-control is-invalid");
                $("#register-password").addClass("form-control is-valid");
                flagPas = true;
            }
        });
        /*验证确认密码*/
        $("#register-passwords").change(function () {
            passWords = $("#register-passwords").val();
            if ((passWord != passWords) || (passWords.length < 6 || passWords.length > 18)) {
                $("#register-passwords").removeClass("form-control is-valid");
                $("#register-passwords").addClass("form-control is-invalid");
                flagPass = false;
            } else {
                $("#register-passwords").removeClass("form-control is-invalid");
                $("#register-passwords").addClass("form-control is-valid");
                flagPass = true;
            }
        });


        $("#regbtn").click(function () {
            if (flagName && flagPas && flagPass) {
                localStorage.setItem("name", name);
                localStorage.setItem("passWord", passWord);
                // location = "login.html"
            } else {
                if (!flagName) {
                    $("#register-username").addClass("form-control is-invalid");
                }
                if (!flagPas) {
                    $("#register-password").addClass("form-control is-invalid");
                }
                if (!flagPass) {
                    $("#register-passwords").addClass("form-control is-invalid");
                }
            }
        })
    })
</script>
</body>
</html>
